﻿using commonImportPlugin.Common;
using ICMS.PluginInterfaces;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace commonImportPlugin.Core.Match
{ 
    public class MatchBase
    {
        CommonUtil commonUtil = CommonUtil.GetInstance();
        /// <summary>
        /// 将两个ICData合并为一个ICData。
        /// 若合并过程同一变量都存在值则以
        /// 第一个ICData的值为主。
        /// </summary>
        /// <param name="icData1">数据1</param>
        /// <param name="icData2">数据2</param>
        /// <returns></returns>
        public ICData MergeICData(ICData icData1,ICData icData2)
        {
            #region 合并两个ICData
            icData1.BankCardID = string.IsNullOrEmpty(icData1.BankCardID) ? icData2.BankCardID : icData1.BankCardID;//银行卡号
            icData1.Extend1 = string.IsNullOrEmpty(icData1.Extend1) ? icData2.Extend1 : icData1.Extend1;//社保文件名
            icData1.Extend2 = string.IsNullOrEmpty(icData1.Extend2) ? icData2.Extend2 : icData1.Extend2;//社保文件头
            icData1.Extend3 = string.IsNullOrEmpty(icData1.Extend3) ? icData2.Extend3 : icData1.Extend3;//金融文件名
            icData1.Extend4 = string.IsNullOrEmpty(icData1.Extend4) ? icData2.Extend4 : icData1.Extend4;
            icData1.Extend5 = string.IsNullOrEmpty(icData1.Extend5) ? icData2.Extend5 : icData1.Extend5;
            icData1.Extend6 = string.IsNullOrEmpty(icData1.Extend6) ? icData2.Extend6 : icData1.Extend6;
            icData1.Extend7 = string.IsNullOrEmpty(icData1.Extend7) ? icData2.Extend7 : icData1.Extend7;
            icData1.Extend8 = string.IsNullOrEmpty(icData1.Extend8) ? icData2.Extend8 : icData1.Extend8;

            icData1.PBOCData = string.IsNullOrEmpty(icData1.PBOCData) ? icData2.PBOCData : icData1.PBOCData;//金融数据
            icData1.PbocDataID = string.IsNullOrEmpty(icData1.PbocDataID) ? icData2.PbocDataID : icData1.PbocDataID;//金融数据Id

            icData1.PersonID = string.IsNullOrEmpty(icData1.PersonID) ? icData2.PersonID : icData1.PersonID;//身份证号

            icData1.SSCardID = string.IsNullOrEmpty(icData1.SSCardID) ? icData2.SSCardID : icData1.SSCardID;//社保卡号
            icData1.SSData = string.IsNullOrEmpty(icData1.SSData) ? icData2.SSData : icData1.SSData;//社保数据

            icData1.Track1 = string.IsNullOrEmpty(icData1.Track1) ? icData2.Track1 : icData1.Track1;//磁条数据
            #endregion
            return icData1;
        }
        /// <summary>
        /// 存在匹配不上的操作处理
        /// </summary>
        /// <param name="matchFails">匹配不上的记录（第一条备注信息)</param>
        /// <param name="errorMessage">错误提示</param>
        /// <param name="orderId">工单号</param>
        /// <param name="notMatchIsContinue">错误是否继续</param>
        /// <returns>true成功</returns>
        public bool FailOperate(List<string> matchFails, string errorMessage, string orderId,bool notMatchIsContinue)
        {
            string showMessage = null;//提示信息

            if (matchFails.Count > 1)
            {
                matchFails[0] = string.Format(errorMessage, matchFails.Count - 1);
                string errorFilePath = string.Format(@"{0}\导入插件匹配结果\{1}_{2}_{3}.txt",
                    Application.StartupPath,orderId, matchFails.Count - 1, DateTime.Now.ToString("yyyy-MM-dd"));

                //可变参数传了false表示直接报错，不给选择是否继续
                if (!notMatchIsContinue)
                {
                    showMessage = string.Format(errorMessage, matchFails.Count - 1);
                    commonUtil.ListStringToTxtFile(matchFails, errorFilePath);//写入
                    bool isOpenFile = commonUtil.WhetherContinue(showMessage + "\n,导入将终止，请确定是否查看日志");
                    if (isOpenFile)
                        commonUtil.OpenFolderAndSelectFile(errorFilePath);
                    throw new Exception("导入失败，存在匹配不上的数据");
                }

                commonUtil.ListStringToTxtFile(matchFails, errorFilePath);//写入
                showMessage = string.Format(errorMessage + "，选择\n“是”将匹配成功的继续导入,忽略匹配不上的,\n“否”：终止导入",
                 matchFails.Count - 1);
                bool isContinue = commonUtil.WhetherContinue(showMessage);
                if (!isContinue)
                {
                    commonUtil.OpenFolderAndSelectFile(errorFilePath);
                    throw new Exception("导入失败，存在匹配不上的数据");
                }                 
            }
            return true;
        }
    }
}
